# -*- coding:utf-8 -*-
# model/model_result.py

from sqlalchemy import Column, DateTime, ForeignKey, String, text
from sqlalchemy.dialects.mysql import DATETIME, INTEGER, SMALLINT, TINYINT
from sqlalchemy.ext.declarative import declarative_base

from utils.utils_func import datetime_to_str


Base = declarative_base()
metadata = Base.metadata


class MainBusiness(Base):
    __tablename__ = 'MainBusiness'

    main_business_id = Column(INTEGER(11), primary_key=True, comment='主健ID')
    name = Column(String(255), nullable=False, comment='主体业务名称')
    create_time = Column(DATETIME, nullable=False, server_default=text("current_timestamp()"), comment='添加时间')
    update_time = Column(DATETIME, comment='更新时间')
    delete_time = Column(DATETIME, comment='删除时间')
    is_deleted = Column(TINYINT(1), nullable=False, server_default=text("0"), comment='是否删除')
    remarks = Column(String(255), comment='主体业务摘要')

    def to_dict(self):
        return {
            "main_business_id": self.main_business_id,
            "name": self.name,
            "create_time": datetime_to_str(self.create_time),
            "update_time": datetime_to_str(self.update_time),
            "delete_time": datetime_to_str(self.delete_time),
            "is_deleted": self.is_deleted,
            "remarks": self.remarks,
        }


class BusinessScenario(Base):
    __tablename__ = 'BusinessScenario'

    business_scenario_id = Column(INTEGER(11), primary_key=True, comment='主键ID')
    main_business_id = Column(INTEGER(11), nullable=False, comment='主体业务ID')
    name = Column(String(255), nullable=False, comment='主体业务场景名称')
    create_time = Column(DateTime, nullable=False, server_default=text("current_timestamp()"), comment='创建时间')
    update_time = Column(DateTime, comment='修改时间')
    delete_time = Column(DateTime, comment='删除时间')
    is_deleted = Column(TINYINT(1), nullable=False, server_default=text("0"), comment='是否删除；1是；0否；')
    remarks = Column(String(255), comment='主体业务场景摘要')

    def to_dict(self):
        return {
            "business_scenario_id": self.business_scenario_id,
            "main_business_id": self.main_business_id,
            "name": self.name,
            "create_time": datetime_to_str(self.create_time),
            "update_time": datetime_to_str(self.update_time),
            "delete_time": datetime_to_str(self.delete_time),
            "is_deleted": self.is_deleted,
            "remarks": self.remarks,
        }


class AppSystem(Base):
    __tablename__ = 'AppSystem'

    app_system_id = Column(INTEGER(11), primary_key=True, comment='主键ID')
    sys_id = Column(String(64), nullable=False, comment='应用系统编号')
    sys_name = Column(String(255), nullable=False, comment='应用系统名称')
    sys_status = Column(SMALLINT(11), nullable=False, server_default=text("1"), comment='系统状态，1：上线，0：下线')
    sys_level = Column(SMALLINT(11), nullable=False, server_default=text("0"), comment='业务系统级别: 0.未定级/1.一级/2.二级/3.三级/4.四级')
    belong_team = Column(String(255), comment='所属团队')
    belong_system = Column(String(255), comment='所属主系统')
    system_type = Column(SMALLINT(11), nullable=False, server_default=text("0"), comment='行内/行外系统: 0.行内系统/1.行外系统')
    create_time = Column(DateTime, nullable=False, server_default=text("current_timestamp()"), comment='创建时间')
    update_time = Column(DateTime, comment='修改时间')
    delete_time = Column(DateTime, comment='删除时间')
    is_deleted = Column(TINYINT(1), nullable=False, server_default=text("0"), comment='是否删除；1是；0否；')
    remarks = Column(String(255), comment='业务系统摘要')

    def to_dict(self):
        return {
            "app_system_id": self.app_system_id,
            "sys_id": self.sys_id,
            "sys_name": self.sys_name,
            "sys_status": self.sys_status,
            "sys_level": self.sys_level,
            "belong_team": self.belong_team,
            "belong_system": self.belong_system,
            "system_type": self.system_type,
            "create_time": datetime_to_str(self.create_time),
            "update_time": datetime_to_str(self.update_time),
            "delete_time": datetime_to_str(self.delete_time),
            "is_deleted": self.is_deleted,
            "remarks": self.remarks,
        }


class DeployManner(Base):
    __tablename__ = 'DeployManner'

    deploy_manner_id = Column(INTEGER(11), primary_key=True, comment='主键ID')
    alias_name = Column(String(64), nullable=False, comment='部署方式')
    create_time = Column(DateTime, nullable=False, server_default=text("current_timestamp()"), comment='创建时间')
    update_time = Column(DateTime, comment='修改时间')
    delete_time = Column(DateTime, comment='删除时间')
    is_deleted = Column(TINYINT(1), nullable=False, server_default=text("0"), comment='是否删除；1是；0否；')
    remarks = Column(String(255), comment='备注')


class AppUser(Base):
    __tablename__ = 'AppUser'

    app_user_id = Column(INTEGER(11), primary_key=True, comment='主键ID')
    user_name = Column(String(64), nullable=False, comment='用户名')
    alias_name = Column(String(64), comment='用户别名')
    create_time = Column(DateTime, nullable=False, server_default=text("current_timestamp()"), comment='创建时间')
    update_time = Column(DateTime, comment='修改时间')
    delete_time = Column(DateTime, comment='删除时间')
    is_deleted = Column(TINYINT(1), nullable=False, server_default=text("0"), comment='是否删除；1是；0否；')
    remarks = Column(String(255), comment='备注')

    def to_dict(self):
        return {
            "app_user_id": self.app_user_id,
            "user_name": self.user_name,
            "alias_name": self.alias_name,
            "create_time": datetime_to_str(self.create_time),
            "update_time": datetime_to_str(self.update_time),
            "delete_time": datetime_to_str(self.delete_time),
            "is_deleted": self.is_deleted,
            "remarks": self.remarks,
        }


class AppSystemDeployMannerMap(Base):
    __tablename__ = 'AppSystemDeployMannerMap'

    app_system_deploy_manner_id = Column(INTEGER(11), primary_key=True, comment='主键ID')
    app_system_id = Column(ForeignKey('AppSystem.app_system_id'), nullable=False, index=True, comment='应用系统ID')
    deploy_manner_id = Column(ForeignKey('DeployManner.deploy_manner_id'), nullable=False, index=True, comment='应用系统ID')


class AppSystemOwnermMap(Base):
    __tablename__ = 'AppSystemOwnermMap'

    app_system_owner_id = Column(INTEGER(11), primary_key=True, comment='主键ID')
    app_system_id = Column(ForeignKey('AppSystem.app_system_id'), nullable=False, index=True, comment='应用系统ID')
    app_user_id = Column(INTEGER(11), nullable=False, comment='应用负责人用户ID')
    owner_role = Column(INTEGER(11), nullable=False, comment='负责人角色: 1.开发/2.数据/3.应用/4.其他')


class BusinessChain(Base):
    __tablename__ = 'BusinessChain'

    busi_chain_id = Column(INTEGER(11), primary_key=True, comment='主键ID')
    business_scenario_id = Column(INTEGER(11), nullable=False, comment='业务场景ID')
    chain_uuid = Column(String(64), nullable=False, comment='业务链UUID')
    sys_id = Column(String(64), nullable=False, comment='应用系统编号')
    sys_sn = Column(INTEGER(11), nullable=False, comment='应用系统业务链序号')

    def to_dict(self):
        return {
            "busi_chain_id": self.busi_chain_id,
            "business_scenario_id": self.business_scenario_id,
            "chain_uuid": self.chain_uuid,
            "sys_id": self.sys_id,
            "sys_sn": self.sys_sn,
        }
